package com.eternal.Exception;

import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 异常返回处理类
 */
@RestControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(ResultException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> handlerRuntimeException(ResultException ex) {
        Map<String,Object> result = new HashMap<>();
        result.put("code",ex.getCode());
        result.put("message", ex.getMessage());
        result.put("data",ex.getData());
        return result;
    }


    @ExceptionHandler(PermissionException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Map<String, Object> handlerRuntimeException(PermissionException ex) {
        Map<String,Object> result = new HashMap<>();
        result.put("code",ex.getCode());
        result.put("message", ex.getMessage());
        return result;
    }

    @ExceptionHandler(LoginException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Map<String, Object> handlerRuntimeException(LoginException ex) {
        Map<String,Object> result = new HashMap<>();
        result.put("code",ex.getCode());
        result.put("message", ex.getMessage());
        return result;
    }


    @ExceptionHandler(AuthorizationException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Map<String, Object> handlerRuntimeException(AuthorizationException ex) {
        Map<String,Object> result = new HashMap<>();
        result.put("code","403");
        result.put("message", ex.getMessage());
        return result;
    }



}
